package com.kaishengit.web;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.kaishengit.entity.Employee;
import com.kaishengit.service.EmployeeService;
import com.kaishengit.service.GoalService;
import com.kaishengit.service.TaskEmployeeService;
import com.kaishengit.service.TaskService;

public class EditTask extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String id = request.getParameter("id");
		
		com.kaishengit.entity.Task task =  new TaskService().findTaskById(id);
		Employee employee = new EmployeeService().findById(task.getEmployeeid());
		request.setAttribute("employee", employee);
		request.setAttribute("task", task);
		request.getRequestDispatcher("WEB-INF/views/edittask.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String id = request.getParameter("id");
		String name = request.getParameter("name");
		String endtime = request.getParameter("endtime");
		String desc = request.getParameter("desc");
		String state = request.getParameter("state");
		String level = request.getParameter("level");
		double rate = Double.parseDouble(request.getParameter("rate"));
		String employeename = request.getParameter("employeename");
		String employeeid =  new EmployeeService().findByName(employeename).getId();
		new TaskService().update(id,name,endtime,desc,state,level,rate,employeeid);
		
		com.kaishengit.entity.Task task = new TaskService().findTaskById(id);
		com.kaishengit.entity.Goal goal = new GoalService().findById(task.getGoalid());
		goal.setRate(goal.getRate()+rate);
		List<com.kaishengit.entity.TaskEmployee> list = new TaskEmployeeService().findByGoalId(task.getGoalid());
		
		

		request.setAttribute("list", list);
		request.setAttribute("goal", goal);
		request.getRequestDispatcher("WEB-INF/views/task.jsp").forward(request, response);
	}

}
